package com.moon.game.command.guest.lair;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;

/**
 * @description 扩充宠物院
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-23 上午10:37:51
 */
public class ExpandPetYardCmd extends BaseCmd{

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		
		Integer ack = Integer.parseInt(ps[0]);
		String info = "";
		if (ack == 0) {
			info = "扩充宠物院需要消耗金币" + Constants.EXTAND_PRICE + "，你确定要扩容吗？(每人宠物院最大容量是：" + Constants.MAX_PET_YARD_CAPACITY + ")";
			req.setVO(TAG_ACK, info);
		}else {
			PlayerVo player = req.getPlayer();
			if (player.getPetYardCapacity() >= Constants.MAX_PET_YARD_CAPACITY) {
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_MAX_PET_YARD_CAPACITY, "你的宠物院容量已经达到最大值，不能再扩容了！"));
				return;
			}
			boolean minusStatu = PlayerService.minusGold(req.getSid(), req.getPlayerId(), Constants.EXTAND_PRICE);
			if (!minusStatu) {
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_GOLD, "你的金币不足" + Constants.MAX_PET_YARD_CAPACITY + "，不能扩充宠物院！"));
				return;
			}
			player.setPetYardCapacity(player.getPetYardCapacity() + 1);
			info = "扩充宠物院成功，消耗金币" + Constants.EXTAND_PRICE + "。(每人宠物院最大容量是：" + Constants.MAX_PET_YARD_CAPACITY + ")";
			req.setVO(TAG_INFO, info);
			
			PlayerService.doUpdatePlayer(req.getSid(), player.getId(), player);
		}
		response(req, req.getClientResponse());
	}

}
